<template>
  <div class="params">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/par' }">y </el-breadcrumb-item>
      <el-breadcrumb-item>分类参数</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-alert title="注意：只允许为第三级分类设置相关参数！" type="warning" show-icon :closable="false"> </el-alert>

      <div class="block">
        <span class="demonstration">选择商品分类：</span>
        <el-cascader expandTrigger="hover" show-all-levels v-model="selectedCateKeys" :options="catelist" :props="cateProps" @change="handleChange"></el-cascader>
      </div>

      <template>
        <el-tabs v-model="activeName" @tab-click="handleClick">
          <el-tab-pane label="动态参数" name="many">
            <el-button type="primary" class="addbtn">添加参数</el-button>
            <el-table class="ttt" :data="manyTableData" border stripe size="mini" style="width: 100%;">
              <el-table-column type="expand">
                <template slot-scope="scope">
                  {{ scope.row.attr_vals }}
                  <el-tag v-for="(item, i) in scope.row.attr_vals" :key="i" closable @close="handleClose(i, scope.row)">{{ item }}</el-tag>
                  <el-input class="input-new-tag" v-if="inputVisible" v-model="inputValue" ref="saveTagInput" size="small" @keyup.enter.native="handleInputConfirm" @blur="handleInputConfirm">
                  </el-input>
                  <el-button v-else class="button-new-tag" size="small" @click="showInput(scope.row)">+ New Tag</el-button>
                </template>
              </el-table-column>
              <el-table-column type="index" width="50"> </el-table-column>
              <el-table-column prop="attr_name" label="参数名称"> </el-table-column>
              <el-table-column prop="" label="操作">
                <template slot-scope="scope">
                  <el-button type="primary" icon="el-icon-edit" size="mini" @click="editById(scope.row.attr_id)"></el-button>
                  <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeById(scope.row.attr_id)"></el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
          <el-tab-pane label="静态参数" name="only">静态参数</el-tab-pane>
        </el-tabs>
      </template>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      catelist: [],
      // 级联选择框的配置对象
      cateProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      // 级联选择框双向绑定到的数组
      selectedCateKeys: [],
      activeName: 'many',
      // 动态参数的数据
      manyTableData: [],
      // 静态属性的数据
      onlyTableData: [],
      dynamicTags: [], //tag数组
      inputVisible: false,
      inputValue: ''
    }
  },
  methods: {
    handleChange() {
      //选级联菜单
      // console.log(value)
      //  console.log(this.selectedCateKeys)
      console.log(this.cateId)
      this.getCategoriesList()
    },
    handleClick(tab) {
      //y页签切换
      console.log(tab)
    },

    // 点击按钮，展示文本输入框
    showInput(row) {
      row.inputVisible = true
      // 让文本框自动获得焦点
      // $nextTick 方法的作用，就是当页面上元素被重新渲染之后，才会指定回调函数中的代码
      this.$nextTick(() => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },
    // 文本框失去焦点，或摁下了 Enter 都会触发
    async handleInputConfirm(row) {
      if (row.inputValue.trim().length === 0) {
        row.inputValue = ''
        row.inputVisible = false
        return
      }
      // 如果没有return，则证明输入的内容，需要做后续处理
      row.attr_vals.push(row.inputValue.trim())
      row.inputValue = ''
      row.inputVisible = false
      // 需要发起请求，保存这次操作
      // this.saveAttrVals(row)
    },

    // 删除对应的参数可选项
    handleClose(i, row) {
      row.attr_vals.splice(i, 1)
      this.saveAttrVals(row)
    },
    getOptionsList() {
      //获取商品列表
      this.$http
        .get('categories')
        .then(result => {
          if (result.data.meta.status !== 200) {
            return this.$message.error('获取列表失败！')
          } else {
            this.catelist = result.data.data
          }
        })
        .catch(err => {
          console.log(err)
        })
      
    },

    getCategoriesList() {
      //获取某商品参数

      this.$http
        .get(`categories/${this.cateId}/attributes`, {
          params: { sel: this.activeName }
        })
        .then(res => {
          let result = res.data

          if (result.meta.status !== 200) {
            return this.$message.error('获取列表失败！')
          } else {
            result.data.forEach(item => {
              item.attr_vals = item.attr_vals ? item.attr_vals.split(',') : []
              // 控制文本框的显示与隐藏
              item.inputVisible = false
              // 文本框中输入的值
              item.inputValue = ''
            })

            if (this.activeName === 'many') {
              this.manyTableData = result.data
            } else {
              this.onlyTableData = result.data
            }
          }
        })
        .catch(err => {
          console.log(err)
        })
    }
  },

  computed: {
    // 当前选中的三级分类的Id
    cateId() {
      if (this.selectedCateKeys.length === 3) {
        return this.selectedCateKeys[2]
      }
      return null
    }
  },
  mounted() {
    this.getOptionsList()
  }
}
</script>
<style lang="less" scoped>
.block {
  height: 105px;
  text-align: left;
  margin-top: -30px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
.el-tabs__header {
  height: 114px;
  margin-top: -50px;
}
.addbtn {
  text-align: left;
  display: block;
}

.el-table__header-wrapper {
  padding: 0;
  height: 40px;
  background: cyan;
  line-height: 40px;
}
.el-table-column {
  height: 40px;
}
.el-table__header th {
  padding: 0;
  height: 40px;
}

.el-table__body td {
  padding: 0;
  height: 40px;
  background: darkmagenta;
}
.el-alert {
  height: 20px;
}
.el-cascader {
  width: 350px;
}
</style>
